package com.example.application1.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.application1.API.countryNews;
import com.example.application1.API.newsApi;
import com.example.application1.Adapter.newsAdapter;
import com.example.application1.Entity.Bean;
import com.example.application1.Entity.NewslistDTO;
import com.example.application1.R;
import com.example.application1.tools.DividerGridItemDecoration;
import com.example.application1.webViewActivity;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class GouNeiNewsFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<NewslistDTO> newsLists;
    private newsAdapter newAdapter;
    private View view;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    public static GouNeiNewsFragment newInstance() {
        GouNeiNewsFragment fragment = new GouNeiNewsFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_news, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        requestData();
        mSwipeRefreshLayout = view.findViewById(R.id.refresh_layout);
        recyclerView = view.findViewById(R.id.recycler_news);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        newAdapter = new newsAdapter(newsLists, getContext());
        recyclerView.addItemDecoration(new DividerGridItemDecoration(getActivity()));
        recyclerView.setAdapter(newAdapter);
        handleDownPullUpdate();

//item点击事件
        newAdapter.setRecyclerItemClickListener(new newsAdapter.OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(int position) {
                Intent intent=new Intent(getActivity(), webViewActivity.class);
                intent.putExtra("url",newsLists.get(position).getUrl());
                getActivity().startActivity(intent);
            }
        });

    }

//    下拉刷新方法
    private void handleDownPullUpdate() {
        mSwipeRefreshLayout.setEnabled(true);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //被刷新时的操作
                requestData();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //更新成功后设置UI，停止更新
                        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                        newAdapter = new newsAdapter(newsLists, getContext());
                        recyclerView.addItemDecoration(new DividerGridItemDecoration(getActivity()));
                        recyclerView.setAdapter(newAdapter);
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                },1000);
            }
        });
    }

//网络数据请求方法
    private void requestData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.tianapi.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
       countryNews service = retrofit.create(countryNews.class);
        Call<Bean> call = service.getCall("1fe590b621dcb1472141d1dee8323c85",20);
        call.enqueue(new Callback<Bean>() {
            @Override
            public void onResponse(Call<Bean> call, Response<Bean> response) {
                newsLists = response.body().getNewslist();
                System.out.println(newsLists.get(0).getUrl());
                newAdapter.refresh(newsLists);

            }
            @Override
            public void onFailure(Call<Bean> call, Throwable t) {
                System.out.println("连接失败");
            }
        });
    }

}